var underscore = require('underscore')
var redisClient = require('../dao/redis/redisClient')

var player = function(opts) {
    //模型id
    this.id = opts.id || 0
    //用户id
    this.userId = opts.userId || 0
    //角色名字
    this.name = opts.name
    //类型 1:人物2:宠物,3：怪物
    this.type = opts.type || 1
    //战斗属性
    this.fightAttri = opts.fightAttri || {}
    // 装备
    this.equipments = opts.equipments ? new Equipments(opts.equipments) : {}
    //基础属性
    this.basicAttri = opts.basicAttri ? new BasicAttri(opts.basicAttri) : {}
    //技能组
    this.skills = opts.skills || []
    //当前经验
    this.exp = opts.exp || 0
    //当前等级
    this.lvl = opts.lvl || 1
    //当前金钱
    this.money = opts.money || 0
    //升级所需经验
    this.lvlexp = opts.lvlexp || 0
    //角色所在的地图
    this.mapId = opts.mapId || 1
    //角色当前挑战的副本
    this.dungeon = opts.dungeon || 0
    //挑战的副本的进度
    this.progress = opts.progress || 1
}

//基础属性
function BasicAttri(opts) {
    //属性点
    this.attriPoint = opts.attriPoint || 0
        //力量
    this.str = opts.str || 0
        //体质
    this.con = opts.con || 0
        //敏捷
    this.dex = opts.dex || 0
        //意志
    this.wil = opts.wil || 0
        //精神
    this.spi = opts.spi || 0
        //血量
    this.maxHp = opts.maxHp || 0
        //攻击力
    this.attack = opts.attack || 0
        //防御
    this.defend = opts.defend || 0
        //暴击伤害
    this.critHurt = opts.critHurt || 0
        //暴击
    this.crit = opts.crit || 0
        //韧性
    this.toughness = opts.toughness || 0
        //命中
    this.hit = opts.hit || 0
        //闪避
    this.avoid = opts.avoid || 0
        //控制
    this.control = opts.control || 0
        //抗性
    this.resistance = opts.resistance || 0
        //无视防御
    this.reduceDefend = opts.reduceDefend || 0
        //无视防御百分比
    this.reduceDefendPer = opts.reduceDefendPer || 0
        //吸血百分比
    this.vampirePer = opts.vampirePer || 0
        //每回合回血
    this.recoverHp = opts.recoverHp || 0
        //伤害减免
    this.reduceDamagePer = opts.reduceDamagePer || 0
        //物理伤害减免
    this.reducePhysicalDamagePer = opts.reduceDamagePer || 0
        //技能伤害减免
    this.reduceSkillDamagePer = opts.reduceDamagePer || 0
}

//角色属性
function FightAttri(opts) {
    //血量
    this.hp = opts.hp || 0
        //最大血量
    this.maxHp = opts.maxHp || 0
        //攻击力
    this.attack = opts.attack || 0
        //防御
    this.defend = opts.defend || 0
        //暴击伤害
    this.critHurt = opts.critHurt || 0
        //暴击
    this.crit = opts.crit || 0
        //韧性
    this.toughness = opts.toughness || 0
        //命中
    this.hit = opts.hit || 0
        //闪避
    this.avoid = opts.avoid || 0
        //控制
    this.control = opts.control || 0
        //抗性
    this.resistance = opts.resistance || 0
        //治疗
    this.cure = opts.cure || 0
        //无视防御
    this.reduceDefend = opts.reduceDefend || 0
        //无视防御百分比
    this.reduceDefendPer = opts.reduceDefendPer || 0
        //吸血百分比
    this.vampirePer = opts.vampirePer || 0
        //每回合回血
    this.recoverHp = opts.recoverHp || 0
        //伤害减免
    this.reduceDamagePer = opts.reduceDamagePer || 0
        //物理伤害减免
    this.reducePhysicalDamagePer = opts.reduceDamagePer || 0
        //技能伤害减免
    this.reduceSkillDamagePer = opts.reduceDamagePer || 0
        //战斗状态
    this.battleStatus = opts.battleStatus || 0
        //状态
    this.status = opts.status || 0 //1:无法回血
        //技能序号
    this.skillPos = opts.skillPos || 0
        //战斗位置
    this.pos = opts.pos || 0
        //技能
    this.skills = opts.skills || 0
        //角色名字
    this.name = opts.name || ''
        //类型 1:人物2:宠物：3：怪物
    this.type = opts.type || 1
        //暴击率
    this.critRate = opts.critRate || 0
        //韧性率
    this.toughnessRate = opts.toughnessRate || 0
        //命中率
    this.hitRate = opts.hitRate || 0
        //闪避率
    this.avoidRate = opts.avoidRate || 0
}

function Equipments(opts) {
    //武器
    this.weanpon = opts.weanpon || 0
        //头部
    this.head = opts.head || 0
        //身体
    this.body = opts.body || 0
        //脚
    this.foot = opts.foot || 0
        //手
    this.hand = opts.hand || 0
        //腰
    this.waist = opts.waist || 0
        //项链
    this.necklace = opts.necklace || 0
        //戒指
    this.ring = opts.ring || 0
}

//装备属性
function equipment(opts) {
    //名字
    this.name = opts.maxHp || 0
        //类型: 1武器2头部3身体4脚5手6腰7项链8戒指
    this.type = opts.type || 0
        //最大血量
    this.maxHp = opts.maxHp || 0
        //攻击力
    this.attack = opts.attack || 0
        //防御
    this.defend = opts.defend || 0
        //暴击伤害
    this.critHurt = opts.critHurt || 0
        //暴击
    this.crit = opts.crit || 0
        //韧性
    this.toughness = opts.toughness || 0
        //命中
    this.hit = opts.hit || 0
        //闪避
    this.avoid = opts.avoid || 0
        //控制命中
    this.control = opts.control || 0
        //控制抗性
    this.resistance = opts.resistance || 0
        //治疗
    this.cure = opts.cure || 0
}

//计算概率
function calcRate(x) {
        var array1 = [
            [0, 1, 0],
            [10, 2, 20],
            [30, 3, 40],
            [60, 4, 60],
            [100, 5, 80],
            [150, 6, 100],
            [210, 7, 120],
            [280, 8, 140],
            [370, 9, 160],
            [470, 10, 180],
            [580, 11, 200],
            [700, 12, 220],
            [830, 13, 240]
        ]
        var array2 = [10, 30, 100, 150, 210, 280, 370, 470, 580, 700, 1320, 830]
        var array3 = [20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240]
        var index = underscore.sortedIndex(array2, x)
        rate = Math.floor((x - array1[index][0]) / array1[index][1]) + array1[index][2]
        return rate
    }
    //升级
player.prototype.lvlup = function() {

    this.basicAttri.control += 1
    this.basicAttri.resistance += 1
    this.basicAttri.hit += 1
    this.basicAttri.avoid += 1
        //力量
    this.basicAttri.str += 1
        //体质
    this.basicAttri.con += 1
        //敏捷
    this.basicAttri.dex += 1
        //意志
    this.basicAttri.wil += 1
        //精神
    this.basicAttri.spi += 1
    this.lvl += 1
}

//升级
player.prototype.lvlupByExp = function() {
    if (this.exp < this.lvlexp) {
        return -1
    }
    this.exp = this.exp - this.lvlexp
    this.lvl += 1
    this.lvlexp = global.lvlexp[this.lvl]
    this.basicAttri.control += 1
    this.basicAttri.resistance += 1
    this.basicAttri.hit += 1
    this.basicAttri.avoid += 1
        //力量
    this.basicAttri.str += 1
        //体质
    this.basicAttri.con += 1
        //敏捷
    this.basicAttri.dex += 1
        //意志
    this.basicAttri.wil += 1
        //精神
    this.basicAttri.spi += 1

}

//计算力量属性
function str(player, num) {
    player.fightAttri.attack += num * 2
}

//计算体质属性
function con(player, num) {
    player.fightAttri.defend += num * 1
    player.fightAttri.maxHp += num * 3
}

//计算意志属性
function wil(player, num) {
    player.fightAttri.critHurt += num * 0.2
    player.fightAttri.resistance += 1
    player.fightAttri.maxHp += num * 1
}

//计算敏捷属性
function dex(player, num) {
    player.fightAttri.crit += num * 2
    player.fightAttri.avoid += num * 1
    player.fightAttri.maxHp += num * 1
}

//计算精神属性
function spi(player, num) {
    player.fightAttri.cure += num * 1
    player.fightAttri.control += num * 2
    player.fightAttri.maxHp += num * 1
}

//计算基础属性
function calcBasicAttri(player) {
    //血量
    player.fightAttri.maxHp += player.basicAttri.maxHp
        //攻击力
    player.fightAttri.attack += player.basicAttri.attack
        //防御
    player.fightAttri.defend += player.basicAttri.defend
        //暴击伤害
    player.fightAttri.critHurt += player.basicAttri.critHurt
        //暴击
    player.fightAttri.crit += player.basicAttri.crit
        //韧性
    player.fightAttri.toughness += player.basicAttri.toughness
        //命中
    player.fightAttri.hit += player.basicAttri.hit
        //闪避
    player.fightAttri.avoid += player.basicAttri.avoid
        //控制
    player.fightAttri.control += player.basicAttri.control
        //抗性
    player.fightAttri.resistance += player.basicAttri.resistance
}

//计算属性概率
function calcAllRate(player) {
    player.fightAttri.critRate = calcRate(player.fightAttri.crit)
    player.fightAttri.toughnessRate = calcRate(player.fightAttri.toughness)
    player.fightAttri.hitRate = calcRate(player.fightAttri.hit)
    player.fightAttri.avoidRate = calcRate(player.fightAttri.avoid)
}

//计算装备属性
function calcEquipment(player) {
    var equipments = player.equipments
    for (var e in equipments) {
        if (equipments[e] !== 0) {
            var equipmentAttri = player.itemInfo[equipments[e]].attri
            for (var a in equipmentAttri) {
                player.fightAttri[a] += equipmentAttri[a]
            }
        }

    }
}

//计算人物属性
player.prototype.calcAttri = function() {
    this.fightAttri = new FightAttri({})
        //人物基础属性
    calcBasicAttri(this)
    str(this, this.basicAttri.str)
    con(this, this.basicAttri.con)
    wil(this, this.basicAttri.wil)
    dex(this, this.basicAttri.dex)
    spi(this, this.basicAttri.spi)
        //技能
        //装备
    calcEquipment(this)
        //属性概率
    calcAllRate(this)
    this.fightAttri.hp = this.fightAttri.maxHp
        //技能
    this.fightAttri.skills = this.skills
    this.fightAttri.name = this.name
    this.fightAttri.type = this.type
}

//保存人物数据
player.prototype.savePlayer = function() {
    var playerStr = JSON.stringify(this, function(key, value) {
        if (key === 'itemInfo' || key === 'fightAttri' || key === 'bag') {
            return undefined
        } else {
            return value
        }
    })
    redisClient.set('p:' + this.id, playerStr)
}

//保存背包数据
player.prototype.saveBag = function() {
    redisClient.set('b:' + this.id, JSON.stringify(this.bag))
}

//保存物品数据
player.prototype.saveItemInfo = function() {
    redisClient.set('i:' + this.id, JSON.stringify(this.itemInfo))
}

player.prototype.recordFight = function(data) {
    var dataStr = JSON.stringify(data)
    redisClient.rpush('lf:' + this.id, dataStr)
    redisClient.ltrim('lf:' + this.id, 0, 9)
}

module.exports = player